<template>
  <Dialog v-model="superVisible" :title="title">
    <el-form ref="superFormRef" :model="superForm" :rules="rules" label-width="120px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="superForm.title" placeholder="请输入标签"></el-input>
      </el-form-item>
      <el-form-item label="问题类型" prop="knowledgeType">
        <el-radio-group v-model="superForm.knowledgeType">
          <el-radio :label="1">一般业务</el-radio>
          <el-radio :label="2">业务问题</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="标签名称" prop="labelName">
        <el-input v-model="superForm.labelName" placeholder="请输入标签"></el-input>
      </el-form-item>
      <el-form-item label="关键词" prop="keys">
        <el-input
          v-model="superForm.keys"
          type="textarea"
          placeholder="请输入关键词,逗号隔开"
        ></el-input>
      </el-form-item>
      <el-form-item label="问法" prop="ask">
        <el-input v-model="superForm.ask" placeholder="请输入问法"></el-input>
      </el-form-item>
      <el-form-item label="问答方式" prop="answerType">
        <el-radio-group v-model="superForm.answerType">
          <el-radio :label="1">单句回答</el-radio>
          <el-radio :label="2">多轮回答</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="问答方式" prop="jumpType">
        <el-select v-model="superForm.jumpType" placeholder="请选择问答方式" class="w-100%">
          <el-option v-for="item in options" :key="item.id" :label="item.label" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="忽略无声" prop="ignoreSilence">
        <el-radio-group v-model="superForm.ignoreSilence">
          <el-radio :label="0">未选</el-radio>
          <el-radio :label="1">忽略</el-radio>
          <el-radio :label="2">跟随主流程一起播放</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否重复播放" prop="enableContinuousPlay">
        <el-radio-group v-model="superForm.enableContinuousPlay">
          <el-radio :label="0">不重复</el-radio>
          <el-radio :label="1">重复</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="播放次数" prop="continuousPlayNum">
        <el-input-number
          v-model="superForm.continuousPlayNum"
          :min="0"
          :max="100"
          placeholder="请输入播放次数"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="superVisible = false">取消</el-button>
      <el-button type="primary" :disabled="fromDisable" @click="submit">确定</el-button>
    </template>
  </Dialog>
</template>

<script setup>
import * as defaultIvrKnowApi from '@/api/call/manage'
const superVisible = ref(false)
const title = ref('知识库问题')
const superForm = reactive({
  id: '',
  title: '', //标题
  knowledgeType: '', //问题类型 1.一般业务 2.业务问题
  labelName: '', //标签名称
  keys: '', //关键词
  ask: '', //问法
  answerType: '', //1单句回答 2.多轮回答
  jumpType: '', //跳转到 1挂机2等待用户应答3下一个主流程4指定主动流程5转人工
  ignoreSilence: '', //忽略无声 0未选,1忽略,2跟随主流程一起播放
  enableContinuousPlay: '', //是否重复播放 0不重复 1重复
  continuousPlayNum: '' //播放次数
})
const options = [
  {
    label: '挂机',
    id: 1
  },
  {
    label: '等待用户应答',
    id: 2
  },
  {
    label: '下一个主流程',
    id: 3
  },
  {
    label: '指定主动流程',
    id: 4
  },
  {
    label: '转人工',
    id: 5
  }
]
const rules = reactive({
  title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
  knowledgeType: [{ required: true, message: '问题不能为空', trigger: 'change' }],
  labelName: [{ required: true, message: '标题名称不能为空', trigger: 'blur' }],
  keys: [{ required: true, message: '关键词不能为空', trigger: 'blur' }],
  ask: [{ required: true, message: '问法不能为空', trigger: 'blur' }],
  answerType: [{ required: true, message: '问答方式不能为空', trigger: 'change' }],
  jumpType: [{ required: true, message: '跳转到不能为空', trigger: 'change' }],
  ignoreSilence: [{ required: true, message: '忽略无声不能为空', trigger: 'change' }],
  enableContinuousPlay: [{ required: true, message: '是否重复播放不能为空', trigger: 'change' }],
  continuousPlayNum: [{ required: true, message: '播放次数不能为空', trigger: 'blur' }]
})
const open = (type, idVal) => {
  superVisible.value = true
  if (idVal) {
    title.value = '编辑知识库问题'
    getDetail(idVal)
  } else {
    superForm.id = ''
    title.value = '创建知识库问题'
    resetForm()
  }
}
const getDetail = async (idVal) => {
  const {
    id,
    title,
    knowledgeType,
    labelName,
    keys,
    ask,
    answerType,
    jumpType,
    ignoreSilence,
    enableContinuousPlay,
    continuousPlayNum
  } = await defaultIvrKnowApi.getIvrDefaultknowDetail(idVal)
  Object.assign(superForm, {
    id,
    title,
    knowledgeType,
    labelName,
    keys,
    ask,
    answerType,
    jumpType,
    ignoreSilence,
    enableContinuousPlay,
    continuousPlayNum
  })
}
const resetForm = () => {
  superForm.title = ''
  superForm.knowledgeType = ''
  superForm.labelName = ''
  superForm.keys = ''
  superForm.ask = ''
  superForm.answerType = ''
  superForm.jumpType = ''
  superForm.ignoreSilence = ''
  superForm.enableContinuousPlay = ''
  superForm.continuousPlayNum = ''
}
const superFormRef = ref(false)
const fromDisable = ref(false)
const emit = defineEmits(['success'])
const submit = async () => {
  try {
    await superFormRef.value.validate()
    fromDisable.value = true
    if (superForm.id) {
      await defaultIvrKnowApi.updateIvrDefaultknow(superForm)
    } else {
      await defaultIvrKnowApi.createIvrDefaultknow(superForm)
    }
    ElMessage.success('操作成功')
    superVisible.value = false
    emit('success')
  } finally {
    fromDisable.value = false
  }
}
defineExpose({ open })
</script>

<style lang="scss" scoped></style>
